/*
 * Copyright (c) 2005, the JUNG Project and the Regents of the University of
 * California All rights reserved.
 *
 * This software is open-source under the BSD license; see either "license.txt"
 * or http://jung.sourceforge.net/license.txt for a description.
 *
 * Created on Aug 18, 2005
 */

package com.davetrudes.jung.client.visualization.util;

import com.davetrudes.jung.client.visualization.event.ChangeEvent;
import com.davetrudes.jung.client.visualization.event.ChangeListener;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;



/**
 * Basic implementation of ChangeEventSupport, using
 * standard jdk classes
 *
 * @author Tom Nelson - tomnelson@dev.java.net
 *
 */
public class DefaultChangeEventSupport implements ChangeEventSupport {

    Object eventSource;
    
    protected EventBus eventBus = new SimpleEventBus();
    
    /**
     * Only one <code>ChangeEvent</code> is needed
     * instance since the
     * event's only state is the source property.  The source of events
     * generated is always "this".
     */
    protected ChangeEvent changeEvent;

    public DefaultChangeEventSupport(Object eventSource) {
        this.eventSource = eventSource;
    }

    public void addChangeListener(ChangeListener l) {
    	eventBus.addHandler(ChangeEvent.TYPE, l);
    }

    public void removeChangeListener(ChangeListener l) {
     //   listenerList.remove(ChangeListener.class, l);
    }
    
    

    /* (non-Javadoc)
	 * @see com.davetrudes.jung.client.visualization.util.ChangeEventSupport#getChangeListeners()
	 */
	@Override
	public ChangeListener[] getChangeListeners() {
		throw new RuntimeException("not implemented");
	}

	/**
     * Notifies all listeners that have registered interest for
     * notification on this event type.  The event instance
     * is lazily created.
     * The primary listeners will be views that need to be repainted
     * because of changes in this model instance
     * @see EventListenerList
     */
    public void fireStateChanged() {
    	eventBus.fireEvent(new ChangeEvent(eventSource));
//        // Guaranteed to return a non-null array
//        Object[] listeners = listenerList.getListenerList();
//        // Process the listeners last to first, notifying
//        // those that are interested in this event
//        for (int i = listeners.length-2; i>=0; i-=2) {
//            if (listeners[i]==ChangeListener.class) {
//                // Lazily create the event:
//                if (changeEvent == null)
//                    changeEvent = new ChangeEvent(eventSource);
//                ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
//            }
//        }
    }

}
